
//
//  MessageViewController.m
//  ProductB
//
//  Created by lanou on 16/5/27.
//  Copyright © 2016年 ChenZheng. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageMainCollectionViewCell.h"
//子类
#import "WeekendTableViewController.h"
#import "LessonTableViewController.h"
#import "NearPlayTableViewController.h"
#import "TastFoodTableViewController.h"
#import "ClubTableViewController.h"
#import "LeisureTableViewController.h"
@interface MessageViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>
/**
 控制器上的scrollerView
 */
@property (nonatomic , strong) UIScrollView *scrollerView;
/**
 collectionView
 */
@property (nonatomic , strong) UICollectionView *collectionView;
/**
 移动的灰色方块
 */
@property (nonatomic , strong) UIView *moveView;
/**
 标题数组
 */
@property (nonatomic , strong) NSArray *titleArray;
/**
 周末
 */
@property (nonatomic , strong) WeekendTableViewController *weekendVC;
/**
 体验课
 */
@property (nonatomic , strong) LessonTableViewController *lessonVC;
/**
 周边
 */
@property (nonatomic , strong) NearPlayTableViewController *nealPlayVC;
/**
 美食
 */
@property (nonatomic , strong) TastFoodTableViewController *tastFoodVC;
/**
 酒吧
 */
@property (nonatomic , strong) ClubTableViewController *clubVC;
/**
 休闲
 */
@property (nonatomic , strong) LeisureTableViewController *leisureVC;
@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //标题数组
    self.titleArray = @[@"周末",@"体验",@"周边",@"美食",@"酒吧",@"休闲"];
    //添加scrollerView
    [self addMyScrollerView];
    //在scrollerView 上添加移动视图
    [self addMoveTableViews];
    //添加collectionView
    [self addCollectionView];

    
}
#pragma mark--------添加scrollerView 的方法----
-(void)addMyScrollerView{
    self.scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, 375, 667 - 30)];
    _scrollerView.delegate = self;
    _scrollerView.contentSize = CGSizeMake(375 * self.titleArray.count, 0);
    _scrollerView.bounces = NO;
    _scrollerView.pagingEnabled = YES;
    _scrollerView.showsHorizontalScrollIndicator = NO;
    _scrollerView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_scrollerView];
}
#pragma mark--------添加移动视图----
-(void)addMoveTableViews{
    // 周末
    self.weekendVC = [[WeekendTableViewController alloc]init];
//    _weekendVC.view.backgroundColor = [UIColor redColor];
    _weekendVC.view.frame = CGRectMake(375 * 0, 0, 375, 667 - 30);
    [self addChildViewController:_weekendVC];
    [self.scrollerView addSubview:_weekendVC.view];
    
    // 体验课
    self.lessonVC = [[LessonTableViewController alloc]init];
//    _lessonVC.view.backgroundColor = [UIColor cyanColor];
    _lessonVC.view.frame = CGRectMake(375 * 1, 0, 375, 667 - 30);
    [self addChildViewController:_lessonVC];
    [self.scrollerView addSubview:_lessonVC.view];
    
    //周边
    self.nealPlayVC = [[NearPlayTableViewController alloc]init];
//    _nealPlayVC.view.backgroundColor = [UIColor redColor];
    _nealPlayVC.view.frame = CGRectMake(375 * 2, 0, 375, 667 - 30);
    [self addChildViewController:_nealPlayVC];
    [self.scrollerView addSubview:_nealPlayVC.view];
    
    //美食
    self.tastFoodVC = [[TastFoodTableViewController alloc]init];
//    _tastFoodVC.view.backgroundColor = [UIColor cyanColor];
    _tastFoodVC.view.frame = CGRectMake(375 * 3, 0, 375, 667 - 30);
    [self addChildViewController:_tastFoodVC];
    [self.scrollerView addSubview:_tastFoodVC.view];
    
    //酒吧
    self.clubVC = [[ClubTableViewController alloc]init];
//    _clubVC.view.backgroundColor = [UIColor greenColor];
    _clubVC.view.frame = CGRectMake(375 * 4, 0, 375, 667 - 30);
    [self addChildViewController:_clubVC];
    [self.scrollerView addSubview:_clubVC.view];
    
    //休闲
    self.leisureVC = [[LeisureTableViewController alloc]init];
//    _leisureVC.view.backgroundColor = [UIColor cyanColor];
    _leisureVC.view.frame = CGRectMake(375 * 5, 0, 375, 667 - 30);
    [self addChildViewController:_leisureVC];
    [self.scrollerView addSubview:_leisureVC.view];

    
}
#pragma mark--------添加collectionView----
-(void)addCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    // 设置为横向滑动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375, 30) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[MessageMainCollectionViewCell class] forCellWithReuseIdentifier:@"MessageMainCollectionViewCell"];
    
    // 创建移动的view
    self.moveView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    self.moveView.alpha = 0.5;
    self.moveView.backgroundColor = [UIColor grayColor];
    self.moveView.layer.cornerRadius = 10;
    self.moveView.layer.masksToBounds = YES;
    [_collectionView addSubview:_moveView];
}
#pragma mark--------collectionView的代理方法----
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MessageMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MessageMainCollectionViewCell" forIndexPath:indexPath];
    cell.label.text = self.titleArray[indexPath.row];
    return cell;
}
#pragma mark --- 点击cell的方法 ---
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != 0)
    {
//        [collectionView setContentOffset:CGPointMake(60 * indexPath.row - 60, 0) animated:YES];
    }
    self.moveView.frame = CGRectMake(60 * indexPath.row, 0, 50, 30);
    [self.scrollerView setContentOffset:CGPointMake(375 * indexPath.row, 0) animated:NO];
}

#pragma mark --- 结束减速触发的方法 ---
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UICollectionView *collectionView = self.view.subviews[1];
    // 如果滚动的不是 collectionView 证明是scrollView 我们需要设置 collectionView的偏移量
    if (scrollView != collectionView)
    {
        if (scrollView.contentOffset.x / 375 != 0)
        {
//            [collectionView setContentOffset:CGPointMake(60 * (scrollView.contentOffset.x / 375) - 60, 0) animated:YES];
        }
    }
}

#pragma mark --- 正在滚动触发的方法 ---
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UICollectionView *collectionView = self.view.subviews[1];
    if (scrollView != collectionView)
    {
        self.moveView.frame = CGRectMake(60 * (scrollView.contentOffset.x / 375), 0, 50, 30);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50, 30);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
